#!/usr/bin/php
<?php
/* Copyright 2017-2025, Dan Landon
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

/* Define our plugin name. */
if (!defined('RECYCLE_PLUGIN')) {
	define('RECYCLE_PLUGIN', 'recycle.bin');
}

/* Define our plugin name. */
if (!defined('DOCROOT')) {
	define('DOCROOT', $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
}

require_once(DOCROOT."/webGui/include/Wrappers.php");

/* Wait a bit before we start. */
sleep(5);

/* If this sxcript is already running or the cron job is running, don't update trashsizes. */
if ((! is_file("/var/run/trashsize.pid")) && (! is_file("/var/run/rc.".RECYCLE_PLUGIN.".pid"))) {
	file_put_contents("/var/run/trashsize.pid", getmypid());

	$trashsize_file			="/tmp/".RECYCLE_PLUGIN."/trashsize";
	$share_trashsize_file	="/tmp/".RECYCLE_PLUGIN."/share_trashsize";

	/* Update /mnt/RecycleBin folder. */
	exec(DOCROOT."/plugins/recycle.bin/scripts/rc.recycle.bin browse");

	/* Get the total recycle bin trashsize. */
	$recycle_bin_trashsize	= exec( "/usr/bin/timeout 60 /usr/bin/du -sc /mnt/RecycleBin/Disk/*/ /mnt/RecycleBin/Flash/* /mnt/RecycleBin/Unassigned/*/ /mnt/RecycleBin/User\ Shares/*/ 2>/dev/null | grep total 2>/dev/null" ) ?? "";
	$recycle_bin_trashsize	= trim(str_replace( 'total', '', $recycle_bin_trashsize));
	$recycle_bin_trashsize	= $recycle_bin_trashsize ? $recycle_bin_trashsize : "0";
	file_put_contents($trashsize_file, $recycle_bin_trashsize);

	/* Get array of share trashsizes. */
	$recycle_bin_cfg = parse_plugin_cfg(RECYCLE_PLUGIN, true);
	$smb_shares = parse_ini_file("/etc/samba/smb-shares.conf", true, INI_SCANNER_RAW);
	$shares = array_keys($smb_shares);
	$share_trashsize = array();

	foreach($shares as $share) {
		$du_share = str_replace("'", "\'", $share);
		$du_share = str_replace(" ", "\ ", $du_share);
		if ($share == 'flash') {
			$recycle_bin_trashsize = exec( "/usr/bin/timeout 10 /usr/bin/du -sc /mnt/RecycleBin/Flash/* 2>/dev/null | grep total 2>/dev/null" );
		} else if (strcmp($share, 'cache') == 0) {
			$recycle_bin_trashsize = exec( "/usr/bin/timeout 30 /usr/bin/du -sc /mnt/RecycleBin/Disk/$du_share/ 2>/dev/null | grep total 2>/dev/null" );
		} else if (strcmp(substr($share, 0, 4), 'disk') != 0) {
			$recycle_bin_trashsize = exec( "/usr/bin/timeout 30 /usr/bin/du -sc /mnt/RecycleBin/User\ Shares/$du_share/* 2>/dev/null | grep total 2>/dev/null" );
		} else {
			$recycle_bin_trashsize = 0;
		}

		$recycle_bin_trashsize = trim(str_replace( 'total', '', $recycle_bin_trashsize));
		if ($recycle_bin_trashsize != "0")
		{
			$share_trashsize[$share]['trashsize'] = $recycle_bin_trashsize;
		}
	}

	$files = glob("/mnt/RecycleBin/Disk/*");
	foreach ($files as $file) {
		$share_name		= basename($file);
		$du_share_name	= str_replace("'", "\'", $share_name);
		$du_share_name	= str_replace(" ", "\ ", $du_share_name);

		$recycle_bin_trashsize = exec( "/usr/bin/timeout 30 /usr/bin/du -sc /mnt/RecycleBin/Disk/$du_share_name/ 2>/dev/null | grep total 2>/dev/null" );

		$recycle_bin_trashsize = trim(str_replace( 'total', '', $recycle_bin_trashsize));

		if ($recycle_bin_trashsize != "0")
		{
			$share_trashsize[$share_name]['trashsize'] = $recycle_bin_trashsize;
		}
	}

	$files = glob("/mnt/RecycleBin/Unassigned/*");
	foreach ($files as $file) {
		$share_name		= basename($file);
		$du_share_name	= str_replace("'", "\'", $share_name);
		$du_share_name	= str_replace(" ", "\ ", $du_share_name);

		$recycle_bin_trashsize = exec( "/usr/bin/timeout 30 /usr/bin/du -sc /mnt/RecycleBin/Unassigned/$du_share_name/* 2>/dev/null | grep total 2>/dev/null" );

		$recycle_bin_trashsize = trim(str_replace( 'total', '', $recycle_bin_trashsize));
		if ($recycle_bin_trashsize != "0")
		{
			$share_trashsize[$share_name]['trashsize'] = $recycle_bin_trashsize;
		}
	}

	$files = glob("/mnt/RecycleBin/Root Shares/*");
	foreach ($files as $file) {
		$share_name		= basename($file);
		$du_share_name	= str_replace("'", "\'", $share_name);
		$du_share_name	= str_replace(" ", "\ ", $du_share_name);

		$recycle_bin_trashsize = exec( "/usr/bin/timeout 30 /usr/bin/du -sc /mnt/RecycleBin/Root\ Shares/$du_share_name/* 2>/dev/null | grep total 2>/dev/null" );

		$recycle_bin_trashsize = trim(str_replace( 'total', '', $recycle_bin_trashsize));
		if ($recycle_bin_trashsize != "0")
		{
			$share_trashsize[$share_name]['trashsize'] = $recycle_bin_trashsize;
		}
	}
	file_put_contents($share_trashsize_file, json_encode($share_trashsize, JSON_PRETTY_PRINT));

	unlink("/var/run/trashsize.pid");
}
?>
